package com.ymatou.iapi.exp.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ymatou.iapi.exp.parameter.AddUserCoinByEvalBean;
import com.ymatou.iapi.exp.parameter.ChangeUserCoinBean;
import com.ymatou.iapi.exp.parameter.Evaluation;
import com.ymatou.iapi.exp.service.AddUserCoinByEvalCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ExpCallService;
import com.ymttest.database.model.UserCoinRecord;
import com.ymttest.database.sqlwapper.ExpWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 主动评价订单获得金币 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AddUserCoinByEval {
	private static AddUserCoinByEvalBean addusercoinbyevalBean;
	private static AddUserCoinByEvalCall addusercoinbyevalCall;
	private static ExpWapper ExpWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("主动评价订单获得金币");
	}

	@Before
	public void caseUp() {
		addusercoinbyevalBean = new AddUserCoinByEvalBean();
		addusercoinbyevalCall = new AddUserCoinByEvalCall();
		ExpWapper = new ExpWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addusercoinbyevalCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * #单个规格满足条件可获得金币数coin=5
	 * 
	 * @param addusercoinbyevalBean2
	 * @param coin
	 */
	private void verifyDB(AddUserCoinByEvalBean addusercoinbyevalBean2, int coin) {
		List<UserCoinRecord> records = ExpWapper.selectUserCoinRecord(addusercoinbyevalBean2.getUserId(), "200103", addusercoinbyevalBean2.getBizNo(), null);
		Logger.debug(FormatUtil.beanToGSONString(records));
		Logger.verifyEquals(true, records.size() > 0, "验证有记录");
		if (records.size() > 0) {
			UserCoinRecord record = records.get(0);
			Logger.verifyEquals(addusercoinbyevalBean2.getUserId(), record.getUserId(), "验证userid");
			Logger.verifyEquals("200103", record.getBizCode().toString(), "验证BizCode");
			Logger.verifyEquals(addusercoinbyevalBean2.getBizNo(), record.getBizNo(), "验证getBizNo");
			Logger.verifyEquals(coin, record.getTotalCoin(), "验证getCoin");
			Logger.verifyEquals(addusercoinbyevalBean2.getBizNo(), record.getOriginalNo(), "验证getOriginalNo");
		}
		Map m=ExpWapper.selectOrderCatalogNum(addusercoinbyevalBean2.getBizNo()).get(0);
		Logger.verifyEquals("1", m.get("EvalStatus").toString(), "验证EvalStatus");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddUserCoinByEval_001() {
		Logger.start(true, "主动评价订单获得金币成功一条记录5金币");
		try {
			// 上传图片+10字
			int userid = 501;
			ExpCallService.resetCoinAccount(userid);
			Long OrderId=Long.valueOf(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			ExpWapper.insertOrderCatalogNum(OrderId, 1);
			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
			addusercoinbyevalCall.setData(addusercoinbyevalBean);
			addusercoinbyevalCall.callService();
			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", addusercoinbyevalCall.getString("BCode"), "验证BCode");
			JsonArray lresult = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("catalogCoinList").getAsJsonArray();
			int coin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("coin").getAsInt();
			int hcoin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("historyCoin").getAsInt();
			Logger.verifyEquals(ExpCallService.EvalCoin, coin, "验证coin");
			Logger.verifyEquals(ExpCallService.EvalCoin, hcoin, "验证historyCoin");
			Logger.verifyEquals(addusercoinbyevalBean.getEvaluationList().size(), lresult.size(), "验证resultSize");
			addusercoinbyevalBean.getEvaluationList().forEach(eval -> {
				String cata = eval.getCatalogId();
				Logger.verifyIsContains(cata, lresult.toString(), "验证数据包含CatalogId");
			});
			verifyDB(addusercoinbyevalBean, ExpCallService.EvalCoin);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddUserCoinByEval_002() {
		Logger.start(true, "主动评价订单获得金币成功多条记录n*5金币");
		try {
			// 上传图片+10字
			int userid = 502;
			ExpCallService.resetCoinAccount(userid);
			Long OrderId=Long.valueOf(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			ExpWapper.insertOrderCatalogNum(OrderId, 1);
			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
			
			Evaluation evaluation = new Evaluation();
			evaluation.setCatalogId(TestDataManager.getRandomUUID().toString());
			evaluation.setContainImage(true);
			evaluation.setTextLength(50);
			addusercoinbyevalBean.getEvaluationList().add(evaluation);
			addusercoinbyevalCall.setData(addusercoinbyevalBean);
			addusercoinbyevalCall.callService();
			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", addusercoinbyevalCall.getString("BCode"), "验证BCode");
			JsonArray lresult = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("catalogCoinList").getAsJsonArray();
			int coin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("coin").getAsInt();
			int hcoin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("historyCoin").getAsInt();
			Logger.verifyEquals(ExpCallService.EvalCoin*2, coin, "验证coin");
			Logger.verifyEquals(ExpCallService.EvalCoin*2, hcoin, "验证historyCoin");
			Logger.verifyEquals(addusercoinbyevalBean.getEvaluationList().size(), lresult.size(), "验证resultSize");
			addusercoinbyevalBean.getEvaluationList().forEach(eval -> {
				String cata = eval.getCatalogId();
				Logger.verifyIsContains(cata, lresult.toString(), "验证数据包含CatalogId");
			});
			verifyDB(addusercoinbyevalBean, ExpCallService.EvalCoin*2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddUserCoinByEval_003() {
		Logger.start(true, "主动评价订单isSubmit为false只返回计算不修改数据");
		try {
			int userid = 503;
			ExpCallService.resetCoinAccount(userid);
			Long OrderId=Long.valueOf(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			ExpWapper.insertOrderCatalogNum(OrderId, 1);
			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
			addusercoinbyevalBean.setSubmit(false);
			Evaluation evaluation = new Evaluation();
			evaluation.setCatalogId(TestDataManager.getRandomUUID().toString());
			evaluation.setContainImage(true);
			evaluation.setTextLength(50);
			addusercoinbyevalCall.setData(addusercoinbyevalBean);
			addusercoinbyevalCall.callService();
			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", addusercoinbyevalCall.getString("BCode"), "验证BCode");
			JsonArray lresult = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("catalogCoinList").getAsJsonArray();
			int coin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("coin").getAsInt();
			int hcoin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("historyCoin").getAsInt();
			//验证返回coin正确 且没有historycoin(说明数据没加到数据库)
			Logger.verifyEquals(ExpCallService.EvalCoin, coin, "验证coin");
			Logger.verifyEquals(0, hcoin, "验证historyCoin");
			Logger.verifyEquals(addusercoinbyevalBean.getEvaluationList().size(), lresult.size(), "验证resultSize");
			addusercoinbyevalBean.getEvaluationList().forEach(eval -> {
				String cata = eval.getCatalogId();
				Logger.verifyIsContains(cata, lresult.toString(), "验证数据包含CatalogId");
			});
			Map m=ExpWapper.selectOrderCatalogNum(addusercoinbyevalBean.getBizNo()).get(0);
			Logger.verifyEquals("0", m.get("EvalStatus").toString(), "验证EvalStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddUserCoinByEval_004() {
		Logger.start(true, "不输入EvaluationList失败");
		try {
			int userid = 504;
			ExpCallService.resetCoinAccount(userid);
			Long OrderId=Long.valueOf(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			ExpWapper.insertOrderCatalogNum(OrderId, 1);
			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
			addusercoinbyevalBean.setEvaluationList(null);
			addusercoinbyevalCall.setData(addusercoinbyevalBean);
			addusercoinbyevalCall.callService();
			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("false", result.get("success").toString(), "验证success");
			Logger.verifyEquals("2", addusercoinbyevalCall.getString("Code"), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddUserCoinByEval_005() {
		Logger.start(true, "不上传图片没有金币获取");
		try {
			int userid = 505;
			ExpCallService.resetCoinAccount(userid);
			Long OrderId=Long.valueOf(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			ExpWapper.insertOrderCatalogNum(OrderId, 1);
			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
			addusercoinbyevalBean.getEvaluationList().get(0).setContainImage(false);
			addusercoinbyevalCall.setData(addusercoinbyevalBean);
			addusercoinbyevalCall.callService();
			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", addusercoinbyevalCall.getString("BCode"), "验证BCode");
			JsonArray lresult = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("catalogCoinList").getAsJsonArray();
			int coin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("coin").getAsInt();
			int hcoin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("historyCoin").getAsInt();
			//验证返回coin正确 且没有historycoin(说明数据没加到数据库)
			Logger.verifyEquals(0, coin, "验证coin");
			Logger.verifyEquals(0, hcoin, "验证historyCoin");
			Logger.verifyEquals(addusercoinbyevalBean.getEvaluationList().size(), lresult.size(), "验证resultSize");
			addusercoinbyevalBean.getEvaluationList().forEach(eval -> {
				String cata = eval.getCatalogId();
				Logger.verifyIsContains(cata, lresult.toString(), "验证数据包含CatalogId");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddUserCoinByEval_006() {
		Logger.start(true, "字数不足没有金币获取");
		try {
			int userid = 505;
			ExpCallService.resetCoinAccount(userid);
			Long OrderId=Long.valueOf(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			ExpWapper.insertOrderCatalogNum(OrderId, 1);
			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
			addusercoinbyevalBean.getEvaluationList().get(0).setTextLength(5);
			addusercoinbyevalCall.setData(addusercoinbyevalBean);
			addusercoinbyevalCall.callService();
			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", addusercoinbyevalCall.getString("BCode"), "验证BCode");
			JsonArray lresult = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("catalogCoinList").getAsJsonArray();
			int coin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("coin").getAsInt();
			int hcoin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("historyCoin").getAsInt();
			//验证返回coin正确 且没有historycoin(说明数据没加到数据库)
			Logger.verifyEquals(0, coin, "验证coin");
			Logger.verifyEquals(0, hcoin, "验证historyCoin");
			Logger.verifyEquals(addusercoinbyevalBean.getEvaluationList().size(), lresult.size(), "验证resultSize");
			addusercoinbyevalBean.getEvaluationList().forEach(eval -> {
				String cata = eval.getCatalogId();
				Logger.verifyIsContains(cata, lresult.toString(), "验证数据包含CatalogId");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddUserCoinByEval_007() {
		Logger.start(true, "重复主动评价订单获得金币失败");
		try {
			// 上传图片+10字
			int userid = 501;
			ExpCallService.resetCoinAccount(userid);
			Long OrderId=Long.valueOf(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
			ExpWapper.insertOrderCatalogNum(OrderId, 1);
			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
			addusercoinbyevalCall.setData(addusercoinbyevalBean);
			addusercoinbyevalCall.callService();
			addusercoinbyevalCall.callService();
			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("false", result.get("success").toString(), "验证success");
			Logger.verifyEquals("4", addusercoinbyevalCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	@Test
//	@Category({ P1.class })
//	@TestCase
//	public void Tc_AddUserCoinByEval_008() {
//		Logger.start(true, "主动评价订单获得金币虚拟商品不会获取");
//		try {
//			PlaceOrderResp placeOrderResp = PlaceOrderService
//					.placeOrderBylogisticsType(LogisticsTypeEnum.AutoDelivery);
//			Logger.debug("placeOrderResp:"+com.ymt.core.tool.FormatUtil.beanToGSONString(placeOrderResp));
//			Long OrderId = Long.valueOf(placeOrderResp.getOrderList().get(0).getOrderId());
//			int  userid= placeOrderResp.getUserId();
//			int tradingId = placeOrderResp.getTradingId();
//			// 上传图片+10字
//			ExpCallService.resetCoinAccount(userid);
//			ExpWapper.insertOrderCatalogNum(OrderId, 1);
//			addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userid,OrderId.toString());
//			addusercoinbyevalCall.setData(addusercoinbyevalBean);
//			addusercoinbyevalCall.callService();
//			JsonObject result = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject();
//			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
//			Logger.verifyEquals("0", addusercoinbyevalCall.getString("BCode"), "验证BCode");
//			JsonArray lresult = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("catalogCoinList").getAsJsonArray();
//			int coin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("coin").getAsInt();
//			int hcoin = addusercoinbyevalCall.getJsonObject().get("Data").getAsJsonObject().get("historyCoin").getAsInt();
//			Logger.verifyEquals(ExpCallService.EvalCoin, coin, "验证coin");
//			Logger.verifyEquals(ExpCallService.EvalCoin, hcoin, "验证historyCoin");
//			Logger.verifyEquals(addusercoinbyevalBean.getEvaluationList().size(), lresult.size(), "验证resultSize");
//			addusercoinbyevalBean.getEvaluationList().forEach(eval -> {
//				String cata = eval.getCatalogId();
//				Logger.verifyIsContains(cata, lresult.toString(), "验证数据包含CatalogId");
//			});
//			verifyDB(addusercoinbyevalBean, ExpCallService.EvalCoin);
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
}